import { formatDate, isString } from '../common/utils';
import { WITHDRAW_LOG_STATUS_TEXT, ACCOUNT_LOG_CATEGORY_TEXT, AWARD_STATUS, TARGET_TYPE } from '../constants/common';

/**
 * 处理资金明细列表
 * @param {Array} list 数据列表
 */
export const dealAccountLogListData = (list) => {
    return list.map(item => {
        const ext = item.ext_info ? (isString(item.ext_info) ? JSON.parse(item.ext_info) : item.ext_info) : {};
        const extInfo = {};
        if (ext.payed) {
            extInfo.dateStr = formatDate(ext.payed, 'YYYY.MM.DD HH:mm');
        } else if (ext.refunded) {
            extInfo.dateStr = formatDate(ext.refunded, 'YYYY.MM.DD HH:mm');
        } else if (ext.finished) {
            extInfo.dateStr = formatDate(ext.finished, 'YYYY.MM.DD HH:mm');
        }
        extInfo.wxpayFee = ext.wxpay_fee;
        extInfo.orderBonus = ext.order_bonus;
        extInfo.orderSerialNumber = ext.order_serial_number;
        extInfo.afterSaleSerialNumber = ext.after_sale_serial_number;
        extInfo.wxLedgerOrderNo = ext.wx_ledger_order_no;
        extInfo.scene = ext.scene || ACCOUNT_LOG_CATEGORY_TEXT[item.category];
        extInfo.spnId = ext.spokesman_id;
        return {
            id: item.id,
            date: item.logged,
            amount: item.amount,
            amountStr: `${item.amount < 0 ? '-' : '+'}￥${Math.abs(item.amount) / 100}`,
            subject: item.subject,
            category: item.category,
            extInfo,
        };
    });
};

/**
 * 处理提现记录列表
 * @param {Array} list 数据列表
 */
export const dealWithdrawLogListData = (list, type = TARGET_TYPE.brand) => {
    return list.map(item => {
        const { brandId, userId, applied, applyState } = item;
        return {
            id: type == TARGET_TYPE.brand ? item.brandAccountWithdrawApplymentId : item.userAccountWithdrawApplymentId,
            date: applied,
            amount: 0 - item.amount,
            subject: item.subject || (type == TARGET_TYPE.brand ? '提现-银行卡' : '提现-微信零钱'),
            state: applyState,
            stateStr: WITHDRAW_LOG_STATUS_TEXT[applyState],
        };
    });
};

/**
 * 处理中奖记录列表
 */
export const dealAwardRecordListData = (list) => {
    return list.map(item => {
        const { awardId, brandId, activityId, activityName, userId, awarded, realPrizeProducts, isRedeemed, redeemDeadlineAt, orderPaymentDeadlineAt, deliveryModes, warehouseId, shippingFee, orderSerialNumber } = item;
        const name = realPrizeProducts.map(i => `${i.productName}*${i.quantity}`).join('、');
        const now = Date.now();
        let status = isRedeemed ? AWARD_STATUS.used : AWARD_STATUS.wait;
        let deadlineDateStr = '';
        if (!isRedeemed) {
            if (now > redeemDeadlineAt) {
                status = AWARD_STATUS.invalid;
            } else if (orderSerialNumber && now <= orderPaymentDeadlineAt) {
                status = AWARD_STATUS.used;
            }
        }
        if (status == AWARD_STATUS.wait) {
            const len = parseInt((redeemDeadlineAt - now) / (24 * 60 * 60 * 1000));
            deadlineDateStr = `${len > 0 ? len + '天后失效' : '今日失效'}`;
        }
        return {
            id: awardId,
            brandId,
            activityId,
            awardId,
            userId,
            name,
            status,
            date: awarded,
            dateStr: formatDate(awarded, 'YYYY.MM.DD HH:mm'),
            deadlineDate: redeemDeadlineAt,
            deadlineDateStr,
            orderNumber: orderSerialNumber,
            activityInfo: {
                name: activityName,
                brandId,
                activityId,
                awardId,
                activityProductWithAttributeDefAndSkus: realPrizeProducts,
                deliveryModes,
                warehouses: [ { id: warehouseId } ],
                unifiedShippingFee: shippingFee,
                minimumPurchaseAmount: 0,
            },
        };
    });
};
